import { NavigationState } from '@react-navigation/native';
import { Platform } from 'react-native';

import { USER_MEMBER_INFO } from '@constants/index';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';

import { setStorageItem } from './storage';
import { memberAPI } from '../api/memberAPI';
dayjs.extend(utc);
/**
 * 查询用户会员信息，并存储到本地缓存
 */
export const getUserMemberInfo = async () => {
  try {
    const res = await memberAPI.getUserMemberInfo();
    console.log('getUserMemberInfo res', res);
    // 本地存储用户会员信息, JSON
    setStorageItem(USER_MEMBER_INFO, JSON.stringify(res));
    // 返回会员信息
    return res.data;
  } catch (error) {
    console.log('getUserMemberInfo error', error);
    // Toast.show({
    //   type: 'error',
    //   text1: 'Failed to obtain member information',
    // });
  }
};

/** 获取当前路由 */
export const getCurrentState = (state: NavigationState) => {
  let currentState = state;

  while (currentState.routes[currentState.index].state) {
    currentState = currentState.routes[currentState.index].state as NavigationState;
  }

  return currentState.routes[currentState.index];
};

export const isAndroid = () => {
  return Platform.OS === 'android';
};

export const isIOS = () => {
  return Platform.OS === 'ios';
};
